package com.aabbc.aaaaa.utils;

import java.io.*;

public class FileServer {
    public static int BLOCK = 10240;

    private FileServer() {}

    public static void pipe(String fileName, OutputStream os) throws Exception {
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(fileName);
            int data = fis.read();
            while (data != -1) {
                os.write(data);
                data = fis.read();
            }
            os.flush();
        } catch (Exception e) {
            throw e;
        } finally {
            try {
                fis.close();
            }catch(Exception e){}
        }
    }

    public static void pipe(File file, OutputStream os) throws Exception {
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(file);
            int data = fis.read();
            while (data != -1) {
                os.write(data);
                data = fis.read();
            }
            os.flush();
        } catch (Exception e) {
            throw e;
        } finally {
            try{
                fis.close();
            }catch(Exception e){}
        }
    }

    public static void pipe(InputStream is, OutputStream os) throws Exception {
        try {
            int data = is.read();
            while (data != -1) {
                os.write(data);
                data = is.read();
            }
            os.flush();
        } catch (Exception e) {
            throw e;
        }
    }

    public static void pipe(InputStream is, File file) throws Exception {
        FileOutputStream os = new FileOutputStream(file);
        try {
            int data = is.read();
            while (data != -1) {
                os.write(data);
                data = is.read();
            }
            os.flush();
        } catch (Exception e) {
            throw e;
        } finally {
            try{
                os.close();
            }catch(Exception e){}
        }
    }

    public static void pipe(byte[] byteArr, OutputStream os) throws Exception {
        os.write(byteArr);
    }

    public static void pipe(byte[] byteArr, File file) throws Exception {
        FileOutputStream os = new FileOutputStream(file);
        try {
            os.write(byteArr);
        } catch (Exception e) {
            throw e;
        } finally {
            try{
                os.close();
            }catch(Exception e){}
        }
    }
}
